// MainActivity.kt
package com.example.experiment2

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private lateinit var linearLayoutContainer: LinearLayout
    private lateinit var editTextQuery: EditText
    private lateinit var btnQuery: Button
    private lateinit var textViewResult: TextView
    private lateinit var model: ProgramAdviserModel
    private var textViewCount = 1  // 记录动态添加的 TextView 数量

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 初始化控件
        editTextQuery = findViewById(R.id.editTextQuery)
        btnQuery = findViewById(R.id.btnQuery)
        textViewResult = findViewById(R.id.textViewResult)
        linearLayoutContainer = findViewById(R.id.linearLayoutContainer)

        // 初始化模型
        model = ProgramAdviserModel()

        // 按钮点击事件：获取查询内容并显示查询结果
        btnQuery.setOnClickListener {
            val query = editTextQuery.text.toString()
            val adviceResourceId = model.getAdviceResourceId(query)  // 调用模型层查询数据
            textViewResult.text = getString(adviceResourceId)  // 更新视图层显示查询结果
        }

        // 动态添加 TextView
        val btnAddTextView: Button = findViewById(R.id.button2)
        btnAddTextView.setOnClickListener {
            val textView = TextView(this)
            textView.text = getString(R.string.dynamic_text, textViewCount)
            textView.textSize = 18f
            textView.setPadding(10, 10, 10, 10)
            textView.setBackgroundColor(0xFFBBDEFB.toInt())  // 设置背景颜色

            // 将新的 TextView 添加到 LinearLayout 中
            linearLayoutContainer.addView(textView)
            textViewCount++  // 增加计数器
        }
    }
}
